package com.briup.cms.service.impl;

import com.briup.cms.bean.User;
import com.briup.cms.dao.UserDao;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.UserService;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public Page<User> getAll(Integer pageNum, Integer pageSize) throws ServiceException {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        return userDao.findAll(pageRequest);
    }

    @Override
    public void saveOrUpdateUser(User user) throws ServiceException {
        //非空
        if (user.getUsername()==null||user.getUsername().trim().equals("")){
            throw  new ServiceException(ResultCode.USER_NAME_ISNULL);
        }
        if (user.getPassword()==null||user.getPassword().trim().equals("")){
            throw  new ServiceException(ResultCode.USER_PWS_ISNULL);
        }
        //唯一
        User user1 = findUserByUsername(user.getUsername());
        if (user1!=null){
            throw new ServiceException(ResultCode.USER_HAS_EXISTED);
        }
        //表中没有要插入的Role
        userDao.save(user);
    }

    @Override
    public void deleteUserInBatch(List<Long> ids) throws ServiceException {
        for (Long id:ids){
            userDao.deleteById(id);
        }
    }

    @Override
    public void deleteUserById(Long id) throws ServiceException {
        User user = findUserById(id);
        if (user==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        userDao.deleteById(id);
    }

    @Override
    public void updateUserStatus(Long id, String status) throws ServiceException {
        User user =userDao.findById(id).orElse(null);
        if (user==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        user.setStatus(status);
        userDao.save(user);
    }

    @Override
    public User login(String username, String password) throws ServiceException {
        //非空
        if (username==null||username.trim().equals("")){
            throw  new ServiceException(ResultCode.USER_NAME_ISNULL);
        }
        if (password==null||password.trim().equals("")){
            throw  new ServiceException(ResultCode.USER_PWS_ISNULL);
        }
        User user = findUserByUsername(username);
        if (user==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        if (!user.getPassword().equals(password.trim())){
            throw new ServiceException(ResultCode.USER_PWS_ERROR);
        }
        if (user.getStatus().equals("1")){
            throw new ServiceException(ResultCode.USER_STATUS_ERROR);
        }
        return user;
    }

    @Override
    public User findUserByUsername(String username) throws ServiceException {
        //非空
        if (username==null||username.trim().equals("")){
            throw  new ServiceException(ResultCode.USER_NAME_ISNULL);
        }

        return userDao.findByUsername(username);
    }

    @Override
    public User findUserById(Long id) throws ServiceException {
        return userDao.findById(id).orElse(null);
    }
}
